package dongze.com.ckdj.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.TableLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import dongze.com.ckdj.R;
import dongze.com.ckdj.adapter.CheckInOperationItemData;
import dongze.com.ckdj.adapter.CheckInOperationMartinTireAdapter;
import dongze.com.ckdj.dagger.component.VehicleComponent;
import dongze.com.ckdj.data.entity.response.VehicleInformationDetail;
import dongze.com.ckdj.databinding.InformationItemTextTablerowBinding;
import dongze.com.ckdj.databinding.model.InformationItemTextModel;
import dongze.com.ckdj.map.MapUtil;
import io.reactivex.observers.DisposableObserver;

/**
 * Created by fanqwz on 2017/9/6.
 */

public class CheckInOperationMartinTireSelectionActivity extends ProgressableActivity {
    MapUtil mapUtil;

    String key;
    String value;
    List<CheckInOperationItemData> list;
    @Inject
    VehicleInformationDetail vehicleInformationDetail;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.basic_info_container)
    TableLayout mBasicInfoLayout;

    DisposableObserver disposable;
    Handler handler;
    CheckInOperationMartinTireAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapUtil = new MapUtil(getAppCompenent());
        handler = new Handler(getMainLooper());

        key = getIntent().getStringExtra("key");
        value = getIntent().getStringExtra("value");
        list = new Gson().fromJson(getIntent().getStringExtra("list"), new TypeToken<List<CheckInOperationItemData>>() {
        }.getType());

        if (getComponent(VehicleComponent.class) != null) {
            getComponent(VehicleComponent.class).inject(this);
        } else {
            finish();
            return;
        }

        if (vehicleInformationDetail == null || TextUtils.isEmpty(key)) {
            new AlertDialog.Builder(this)
                    .setMessage("传入数据缺失")
                    .setCancelable(false)
                    .setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    .create()
                    .show();
        }

        setContentView(R.layout.activity_check_in_operation_image_item_selection);
        ButterKnife.bind(this);
        adapter = new CheckInOperationMartinTireAdapter(list, value);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setAdapter(adapter);

        InformationItemTextModel vin = new InformationItemTextModel();
        vin.setKey("车架号");
        vin.setValue(vehicleInformationDetail.getVin());
        addBasicInfoItem(vin);

        InformationItemTextModel item = new InformationItemTextModel();
        item.setKey("点检项");
        item.setValue(key);
        addBasicInfoItem(item);
    }

    @Override
    protected void onDestroy() {
        mapUtil.dispose();
        super.onDestroy();
    }

    private void dataOk() {
        Intent intent = new Intent();
        intent.putExtra("key", key);
        intent.putExtra("value", adapter.getValue());

        if (TextUtils.isEmpty(adapter.getValue())) {
            setResult(RESULT_CANCELED, intent);
            Toast.makeText(this, "没有输入数据", Toast.LENGTH_SHORT).show();
        } else {
            setResult(RESULT_OK, intent);
        }

        finish();
    }

    private void addBasicInfoItem(InformationItemTextModel model) {
        InformationItemTextTablerowBinding binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.information_item_text_tablerow, mBasicInfoLayout, false);
        binding.getRoot().setTag(binding);
        binding.setVehicleInformationItemText(model);
        mBasicInfoLayout.addView(binding.getRoot());
    }
}
